package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Business;
import model.BusinessTypeEnum;
import model.Course;
import model.CourseHistory;
import model.FreeStudent;
import model.OfferedCourse;
import model.Registration;
import persistency.ArgIO;

public class OfferedCourseController extends BusinessController{
	private StringBuilder callableStatement;

	private static OfferedCourseController instance = new OfferedCourseController();
	private OfferedCourseController(){}
	
	public static OfferedCourseController instance(){
		return instance;
	}
	
	public boolean insertOfferedCourse(OfferedCourse offeredCourse) {
		callableStatement = new StringBuilder("{call sp_insertofferedcourse(?,?,?,?,?,?,?,?,?)}");
		return insert(offeredCourse,callableStatement,9);
	}
	
	
	
	public OfferedCourse getOfferedCourse(Object id){
		return (OfferedCourse)getBusinessObject(id,new StringBuilder("{call sp_getbyidofferedcourse(?)}"),BusinessTypeEnum.OFFEREDCOURSE);
	}
	
	public OfferedCourse getOfferedCourseByRegistrationId(Object id){
		return (OfferedCourse)getBusinessObject(id,new StringBuilder("{call sp_getbyregistrationidofferedcourse(?)}"),BusinessTypeEnum.OFFEREDCOURSE);
	}
	
	public Collection<OfferedCourse> getByCourseIDOfferedCourses(int id) {
		Collection<OfferedCourse> col = new ArrayList<OfferedCourse>();
		col.addAll((Collection<? extends OfferedCourse>) getBusinessObjects(id,new StringBuilder("{call sp_getbycourseidofferedcourses(?)}"),BusinessTypeEnum.OFFEREDCOURSE,java.sql.Types.INTEGER));
		return col;
	}
	
	public Collection<OfferedCourse> getByTeacherIdOfferedCourses(String id) {
		Collection<OfferedCourse> col = new ArrayList<OfferedCourse>();
		col.addAll((Collection<? extends OfferedCourse>) getBusinessObjects(id,new StringBuilder("{call sp_getbyteacheridofferedcourses(?)}"),BusinessTypeEnum.OFFEREDCOURSE,java.sql.Types.CHAR));
		return col;
	}
	
	public Collection<OfferedCourse> getAll() {
		Collection<OfferedCourse> col = new ArrayList<OfferedCourse>();
		col.addAll((Collection<? extends OfferedCourse>) getBusinessObjects(new StringBuilder("{call sp_getallofferedcourse}"),BusinessTypeEnum.OFFEREDCOURSE));
		return col;
	}
	
	
	

	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		
		/*
    in coursecode integer,
    in classroom integer,
    in teachercode varchar(25),
    in numberofplaces integer,
    in day varchar(25),
    in starthour integer,
    in startminutes integer,
    in duration integer,
    in state varchar(25)
		 */
		
		OfferedCourse offeredCourse = (OfferedCourse) business;
		switch (i) {
		case 0: {
			args[i] = offeredCourse.getCourse().getCode();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = offeredCourse.getClassroom().getValue();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 2: {
			args[i] = offeredCourse.getTeacher().getTeacherCode();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 3: {
			args[i] = offeredCourse.getNumberOfPlaces();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 4: {
			args[i] = offeredCourse.getTimeBlock().getDay().toString();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 5: {
			args[i] = offeredCourse.getTimeBlock().getHour();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 6: {
			args[i] = offeredCourse.getTimeBlock().getMinute();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 7: {
			args[i] = offeredCourse.getTimeBlock().getDuration();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 8: {
			args[i] = offeredCourse.getState().toString();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}

	
}
